<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>系统登录</title>
    <script src="/js/vue.min.js"></script>
    <link rel="stylesheet" href="/elementUI/index.css">
    <script src="/elementUI/index.js"></script>
    <script src="/js/axios.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", Arial, sans-serif;
            background-color: #f5f7fa;
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .login-container {
            width: 400px;
            background-color: #fff;
            border-radius: 5px;
            box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
            padding: 30px;
            position: relative;
            top: -80px;
        }

        .login-title {
            text-align: center;
            font-size: 24px;
            color: #409EFF;
            margin-bottom: 30px;
            font-weight: bold;
        }

        .login-form {
            width: 100%;
        }

        .login-button {
            width: 100%;
            margin-top: 10px;
        }

        .login-tips {
            font-size: 12px;
            color: #909399;
            text-align: center;
            margin-top: 20px;
        }

        .copyright {
            text-align: center;
            color: #909399;
            font-size: 12px;
            margin-top: 20px;
        }

        .demo-ruleForm {
            padding: 0 20px;
        }
    </style>
</head>

<body>
<div id="app">
    <div class="login-container">
        <h2 class="login-title">系统登录</h2>
        <el-form :model="loginForm" :rules="rules" ref="loginForm" class="demo-ruleForm">
            <el-form-item prop="username">
                <el-input v-model="loginForm.username" placeholder="用户名" prefix-icon="el-icon-user"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="loginForm.password" type="password" placeholder="密码" prefix-icon="el-icon-lock"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('loginForm')" class="login-button" :loading="loading">登录</el-button>
            </el-form-item>
        </el-form>
        <!--
        <div class="login-tips">
            提示：测试用户名: admin，密码: 123456
        </div>> -->

        <div class="copyright">
            © 2023 后台管理系统 All Rights Reserved
        </div> </div>
</div>



<script>
    new Vue({
        el: '#app',
        data() {
            return {
                loginForm: {
                    username: '',
                    password: ''
                },
                rules: {
                    username: [{
                        required: true,
                        message: '请输入用户名',
                        trigger: 'blur'
                    }],
                    password: [{
                        required: true,
                        message: '请输入密码',
                        trigger: 'blur'
                    }]
                },
                loading: false
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.loading = true;
                        // 使用axios发送登录请求
                        axios.post('/users/login', this.loginForm)
                            .then(response => {
                                this.loading = false;
                                // 假设返回数据格式为 { code: 200, message: '成功', data: { token: 'xxx' } }
                                if (response.data.code === 200) {
                                    this.$message({
                                        message: '登录成功！正在跳转...',
                                        type: 'success'
                                    });
                                    // 在实际应用中，这里可以存储token等用户信息
                                    // 然后跳转到其他页面
                                    setTimeout(() => {
                                        // 这里模拟跳转到首页
                                        window.location.href = '/home';
                                    }, 1000);
                                } else {
                                    this.$message.error(response.data.message || '登录失败');
                                }
                            })
                            .catch(error => {
                                this.loading = false;
                                if (error.response) {
                                    // 请求已发出，但服务器响应的状态码不在 2xx 范围内
                                    this.$message.error(error.response.data.message || '登录失败');
                                } else {
                                    this.$message.error('请求失败，请检查网络连接');
                                }
                            });
                    } else {
                        return false;
                    }
                });
            }
        }
    });
</script>
</body>

</html>